package com.ktjy.service.impl;

import com.ktjy.dao.PetDao;
import com.ktjy.dao.impl.PetDaoMySQLImpl;
import com.ktjy.entity.Pet;
import com.ktjy.service.PetService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @return 13141
 * 2025/6/19
 * 08:49
 */
@Service("petService")
public class PetServiceImpl implements PetService {
    // 创建 PetDao 实现对象
//    PetDao petDao = new PetDaoMySQLImpl();

    // 通过指定的名称获取 PetDao 实例
//    PetDao petDao = PetDaoFactory.getInstance(PetDaoFactory.defaultBeanName);
    // 提供 petDao 属性和对应的 getter, setter
    @Resource(type = PetDaoMySQLImpl.class)
    private PetDao petDao;

    public PetDao getPetDao() {
        return petDao;
    }

//    public void setPetDao(PetDao petDao) {
//        this.petDao = petDao;
//    }

    @Override
    public boolean save(Pet pet) {
        int rows = petDao.insert(pet);
        delay(3000); // 模拟执行业务 3 秒，注意：这里把抛出异常代码删除
        return rows > 0;
    }

    @Override
    public boolean modify(Pet pet) {
        delay(2000); // 模拟执行业务 2 秒
        return false;
    }

    @Override
    public boolean del(Integer id) {
        delay(1000); // 模拟执行业务 1 秒
        return false;
    }
    // 模拟业务执行所需的时间
    private void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
